package com.aizhixin.lab.post.server;

import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.post.data.SkillPointsData;
import com.aizhixin.lab.post.domain.SkillPointsDomain;
import com.aizhixin.lab.post.entity.PostSkill;
import com.aizhixin.lab.post.entity.SkillColor;
import com.aizhixin.lab.post.entity.SkillPoints;
import com.aizhixin.lab.post.repository.SkillPointsRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class SkillPointsService {
    @Autowired
    private SkillPointsRepository pointsRepository;
    @Autowired
    private SkillColorService skillColorService;
    public SkillPoints save(SkillPoints skillPoints){
        return pointsRepository.save(skillPoints);
    }
    public List<SkillPoints> save(List<SkillPoints> pointsList){
        return pointsRepository.save(pointsList);
    }
    public Integer delete(String skillId){
        return pointsRepository.deleteBySkillId(skillId);
    }

    public List<SkillPoints> findSkillPoints(String id){
        return pointsRepository.findBySkillId(id);
    }
    public List<SkillPointsData> findSkillPointsList(String skillId){
        List<SkillColor> all = skillColorService.findAll();
        Map<String,SkillColor> map=new HashMap<>();
        for (SkillColor skillColor:all){
            map.put(skillColor.getId(),skillColor);
        }
        List<SkillPoints> skillPointsList = pointsRepository.findBySkillId(skillId);
        List<SkillPointsData> domainList=new ArrayList<>();
        for (SkillPoints skillPoints:skillPointsList){
            SkillPointsData pointsData = new SkillPointsData();
            BeanUtils.copyProperties(skillPoints,pointsData);
            SkillColor skillColor = map.get(skillPoints.getSkillColorId());
            if (!Objects.isNull(skillColor)) {
                pointsData.setColourNumber(skillColor.getColourNumber());
                pointsData.setSkillPointsLevel(skillColor.getColor());
            }
            domainList.add(pointsData);
        }
        return domainList;
    }

    public List<SkillPoints> findList(List<String> idList){
        return pointsRepository.findAll(idList);
    }

    public SkillPoints deletePoints(String id){
        SkillPoints skillPoints = pointsRepository.findOne(id);
        skillPoints.setDeleteFlag(DataValidity.INVALID.getState());
        return pointsRepository.save(skillPoints);
    }
    public String saveDomain(SkillPointsDomain domain){
        SkillPoints skillPoints = new SkillPoints();
        BeanUtils.copyProperties(domain,skillPoints);
        skillPoints.setId(UUID.randomUUID().toString());
        skillPoints.setDeleteFlag(DataValidity.VALID.getState());
        pointsRepository.save(skillPoints);
        return "OK";
    }
    public String updateDomain(SkillPointsDomain domain){
        SkillPoints skillPoints = pointsRepository.findOne(domain.getId());
        BeanUtils.copyProperties(domain,skillPoints);
        pointsRepository.save(skillPoints);
        return "OK";
    }
}
